package com.fishery.processing.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.mapper.CraftMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author run
 * @since 2021/1/14 16:40
 */
@Component
public class CraftDao {

    @Autowired
    private CraftMapper craftMapper;

    public int addCraft(Craft craft){
        return craftMapper.insert(craft);
    }

    public int removeCraft(String craftId){
        return craftMapper.deleteById(craftId);
    }

    public int updateCraft(Craft craft){
        return craftMapper.updateById(craft);
    }

    public Craft findCraftById(String craftId){
        return craftMapper.selectById(craftId);
    }

    //获取加工产品的工艺数量
    public long getCraftCountOfProduct(String productId){
        return craftMapper.getCraftCountOfProduct(productId);
    }

    //通过工艺名称获取工艺
    public List<Craft> findCraftByFactoryAndName(Craft craft){
        Map<String,Object> map = new HashMap<>(2,1);
        map.put("processing_factory_id",craft.getProcessingFactoryId());
        map.put("craft_name",craft.getCraftName());
        return craftMapper.selectByMap(map);
    }

    //给加工产品添加工艺
    public int addCraftOfProduct(String productId,String craftId,Integer sort){
        craftMapper.increaseSort(productId,sort);
        return craftMapper.addCraftOfProduct(productId,craftId,sort);
    }

    //修改加工产品的工艺
    public int updateCraftOfProduct(String productId,String craftId,Integer sort){
        return craftMapper.updateCraftOfProduct(productId, craftId, sort);
    }

    //删除加工产品的指定工艺
    public int removeCraftOfProduct(String productId,Integer sort){
        int result = craftMapper.removeCraftOfProduct(productId, sort);
        craftMapper.decreaseSort(productId,sort);
        return result;
    }

    //删除指定加工产品与其所有工艺的连接信息
    public int removeAllCraftOfProduct(String productId){
        return craftMapper.removeAllCraftOfProduct(productId);
    }

    //删除指定加工厂的所有工艺
    public int removeCraftOfFactory(String factoryId){
        Map<String,Object> map = new HashMap<>(1,1);
        map.put("processing_factory_id",factoryId);
        return craftMapper.deleteByMap(map);
    }

    public IPage<Craft> findCraftsOfFactory(String factoryId, Integer page, Integer size){
        QueryWrapper<Craft> wrapper = new QueryWrapper<>();
        wrapper.eq("processing_factory_id",factoryId);
        Page<Craft> craftPage = new Page<>(page,size);
        return craftMapper.selectPage(craftPage,wrapper);
    }

    //获取加工厂的全部工艺
    public List<Craft> findCraftsOfFactory(String factoryId){
        Map<String,Object> map = new HashMap<>(1,1);
        map.put("processing_factory_id",factoryId);
        return craftMapper.selectByMap(map);
    }

    //手动封装分页
    public IPage<Craft> findCraftsOfProduct(String productId,Integer page,Integer size) {
        Page<Craft> craftPage = new Page<>(page, size);
        long total = getCraftCountOfProduct(productId);
        return craftPage.setRecords(craftMapper.findCraftsOfProduct(productId, Math.max((page - 1) * size,0), size))
                        .setTotal(total)
                        .setPages((int)(Math.ceil(total / (size * 1.0))));
    }

    //获取加工产品的全部工艺信息
    public List<Craft> findAllCraftsOfProduct(String productId){
        return craftMapper.findAllCraftsOfProduct(productId);
    }


}
